<?php

#Cut Word 
function truncateStr($str, $maxChars='',$case = ""){

    if (strlen($str) > $maxChars ){
			$str = iconv_substr($str, 0, $maxChars,"UTF-8");
	} 
	return $str.$case;
} 

function avai($v,$ol) 	{
	$av="<a class='textunderline' href='$ol' target='_blank' rel='nofollow'>Compare prices for ".$v."&nbsp;Sellers</a>";
	return $av;
}

function item_get_image140($item, $maxwidth = 140) {
	$image = $item->LargeImage;	
	return item_trim_image($image, $maxwidth);
}

function clean($var) {
	$switch_chars = '(,),\,/,<,>,&,$';
	$sc = explode(',', $switch_chars);
	foreach ($sc as $c) $var = str_replace($c, '', $var);
	$var = preg_replace('/[^[:alnum:]\s-]+/', '', $var);
	$var = preg_replace('/\s+/', ' ', $var); 
	$var = str_replace('"', '', $var);
	$var = trim($var); 
  return $var;
}

function merchant_get_price($var) {
	if( $var == 'Too low to display'){
				$price = 	"Check";
	} else {
				$price = 	$var ;
	}
	return $price;
}

function item_list_price($item) {
	if( count($item->Offers->Offer) > 0 ) $price = $item->ItemAttributes->ListPrice->FormattedPrice;
	if( !$price || $price == 'Too low to display') $price = "Check";
	return $price;
}

function item_low_price($item) {
	if( count($item->Offers->Offer) > 0 ) $price = $item->Offers->Offer->OfferListing->Price->FormattedPrice;
	if( !$price || $price == 'Too low to display') $price = "Limited time offer";
	return $price;
}

function item_lookup($asin,$opt, $moremerchant = false)
{
		if ($moremerchant == true) {
		$merchant = "MerchantId=Featured&";
		}
			$request_dirty = 'Operation=ItemLookup&'.$merchant.
					'ItemId=' . $asin .
					'&ResponseGroup='. $opt;
			$request2 = amazon_request($request_dirty);
			$response2 = amazon_get_url($request2);
			$response2 = simplexml_load_string($response2);
			return $response2;
}


#Function show rating
function start_rating($number) {

	if ($number > 4.79)
		return "5";

	if ($number > 4.49)
		return "466";

	if ($number > 4.19)
		return "433";

	if ($number > 3.79)
		return "4";

	if ($number > 3.49)
		return "366";

	if ($number > 3.19)
		return "333";

	if ($number > 2.79)
		return "3";

	if ($number > 2.49)
		return "266";

	if ($number > 2.19)
		return "233";

	if ($number > 1.79)
		return "2";

	if ($number > 1.49)
		return "166";

	if ($number > 1.19)
		return "133";

	if ($number > 0.79)
		return "1";

	if ($number > 0.49)
		return "066";

	if ($number > 0.19)
		return "033";

	return "433";
}

function compare($amazon_asin,$offerlink){
		global $home_url,$amazon_acc;
		//$opt = "Medium,MerchantItemAttributes,OfferFull";
		$opt = "Medium,OfferFull";
		$parsed_xml2 = item_lookup($amazon_asin,$opt,true);

		$item2 = $parsed_xml2->Items->Item;

		$TotalMercant=$item2->Offers->TotalOffers;
		$id=1;
		if ($TotalMercant=="0") {  	
			$store_link_table .='
				<table  id="item_mlink_table">
					<tbody>
						<tr class="custom_tr_top">
								<td class="custom_td_top"></td>
								<td class="custom_td_top">Out of stock</td>
								<td class="custom_td_top"></td>
								<td class="custom_td_top"></td>
						</tr>
					</tbody>
				</table>
			';
		}        
		else {
			$store_link_table .= '
			<table  id="item_mlink_table">
			
			<thead>
			<tr class="custom_tr_top">
					
					<td class="custom_td_top">Seller</td>
					<td class="custom_td_top">Seller Rating</td>
					<td class="custom_td_top">Price</td>
					<td class="custom_td_top">Availability</td>
					<td class="custom_td_top" width="30"></td>
					
			</tr>
			</thead>
			<tbody>
			';
			
			foreach ($item2->Offers->Offer as $itemoffer){
			
				$trcount= $id; $id++;
				$Offer_name=$itemoffer->Merchant->Name;
				$Offer_link=$itemoffer->Merchant->GlancePage;
				$Offer_Rating=$itemoffer->Merchant->AverageFeedbackRating;
				$TotalFeedback = $itemoffer->Merchant->TotalFeedback;
				$Offer_OfferAttributes	=	$itemoffer->OfferAttributes->Condition;
				$Offer_OfferAttributes_note	=	$itemoffer->OfferAttributes->ConditionNote;
				$Offer_OfferListingId	=	$itemoffer->OfferListing->OfferListingId; 
				$Offer_ExchangeId	=	$itemoffer->OfferListing->ExchangeId; 
				$Offer_Price =$itemoffer->OfferListing->Price->FormattedPrice; 
				$Offer_AmountSaved=$itemoffer->OfferListing->AmountSaved->FormattedPrice;
				if ($Offer_name!="Amazon.com"){
					$store_link_table .='<tr class="custom_tr" style="border-bottom: medium none;">';
					//$store_link_table .='<td><a rel="nofollow" href="'.$offerlink.'&amp;id='. $trcount.'"><img src="'.$home_url.'/images/seeit-button.png" alt="See more detail" /></a></td>';
					
					
					
					$store_link_table .='<td>'.$Offer_name.'</td>';
					//$store_link_table .='<td><img src="'.$home_url.'/images/rate/'.start_rating($Offer_Rating).'stars.png" alt="Seller Rating" /><div class="custom_numreviews"><a rel="nofollow" href="'.$Offer_link.'&amp;id='. $trcount.'">'.$TotalFeedback.' Reviews</a></div></td>';
					$store_link_table .='<td><img src="'.$home_url.'/images/rate/'.start_rating($Offer_Rating).'stars.png" alt="Seller Rating" /><div class="custom_numreviews">'.$TotalFeedback.' Reviews</div></td>';
					//$store_link_table .='<td><a rel="nofollow" href="'.$offerlink.'&amp;id='. $trcount.'"><span class="price">'.merchant_get_price($Offer_Price).'</span></a></td>';
					$store_link_table .='<td><span class="price">'.merchant_get_price($Offer_Price).'</span></td>';
					$store_link_table .='<td>In Stock</td>';
					
					$store_link_table .='<td><a href="javascript:void(0)" onclick=location="'.$offerlink.'&amp;id='. $trcount.'"><img src="'.$home_url.'/images/seeit-button.png" alt="See more detail" /></a></td>';
					//$store_link_table .='<td><a rel="nofollow" href="'.$Offer_link.'&tag='.$amazon_acc.'">'.$Offer_name.'</a></td>';
					
					
					
					$store_link_table .='</tr>';
				} else {
					$store_link_table .='<tr class="custom_tr" style="border-bottom: medium none;">';
					
					$store_link_table .='<td><a href="javascript:void(0)" onclick=location="'.$offerlink.'&amp;id='. $trcount.'"><img src="'.$home_url.'/images/trust.jpg" alt="See more detail" /></a></td>';
					
					$store_link_table .='<td><img src="'.$home_url.'/images/rate/5stars.png" alt="Seller Rating" /><div class="custom_numreviews"></div></td>';
					//$store_link_table .='<td><a rel="nofollow" href="'.$offerlink.'&amp;id='. $trcount.'"><span class="price">'.merchant_get_price($Offer_Price).'</span></a></td>';
					$store_link_table .='<td><span class="price">'.merchant_get_price($Offer_Price).'</span></td>';
					$store_link_table .='<td>In Stock</td>';
					
					
					$store_link_table .='<td><a href="javascript:void(0)" onclick=location="'.$offerlink.'&amp;id='. $trcount.'"><img src="'.$home_url.'/images/seeit-button.png" alt="See more detail" /></a></td>';
					//$store_link_table .='<td><a rel="nofollow" href="'.$Offer_link.'&tag='.$amazon_acc.'">'.$Offer_name.'</a></td>';
					
					$store_link_table .='</tr>';
				}
				
			}
			$store_link_table .= " </tbody></table> \n";  
		}      
		$stories .= $store_link_table;
		return $stories;
}



function compare_rating($amazon_asin,$offerlink){
		global $home_url,$amazon_acc;
		//$opt = "Medium,MerchantItemAttributes,OfferFull";
		$opt = "Medium,OfferFull";
		$parsed_xml2 = item_lookup($amazon_asin,$opt,true);

		$item2 = $parsed_xml2->Items->Item;

		$TotalMercant=$item2->Offers->TotalOffers;
		$id=1;
		if ($TotalMercant=="0") {  	
			$store_link_table .='

			';
		}        
		else {
			
			foreach ($item2->Offers->Offer as $itemoffer){
			
				$trcount= $id; $id++;
				$Offer_name=$itemoffer->Merchant->Name;
				$Offer_link=$itemoffer->Merchant->GlancePage;
				$Offer_Rating=$itemoffer->Merchant->AverageFeedbackRating;
				$TotalFeedback = $itemoffer->Merchant->TotalFeedback;
				$Offer_OfferAttributes	=	$itemoffer->OfferAttributes->Condition;
				$Offer_OfferAttributes_note	=	$itemoffer->OfferAttributes->ConditionNote;
				$Offer_OfferListingId	=	$itemoffer->OfferListing->OfferListingId; 
				$Offer_ExchangeId	=	$itemoffer->OfferListing->ExchangeId; 
				$Offer_Price =$itemoffer->OfferListing->Price->FormattedPrice; 
				$Offer_AmountSaved=$itemoffer->OfferListing->AmountSaved->FormattedPrice;
				if ($Offer_name!="Amazon.com"){
					$store_link_table ='<img src="'.$home_url.'/images/rate/'.start_rating($Offer_Rating).'stars.png" alt="Seller Rating from '.$TotalFeedback.' Reviews"  title="Seller Rating from '.$TotalFeedback.' Reviews" />';
				}
				
			} 
		}      
		$stories .= $store_link_table;
		return $stories;
}

//for list
function compare_rating2($amazon_asin,$offerlink){
		global $home_url,$amazon_acc,$link_cloak;
		//$opt = "Medium,MerchantItemAttributes,OfferFull";
		$opt = "Medium,OfferFull";
		$parsed_xml2 = item_lookup($amazon_asin,$opt,true);

		$item2 = $parsed_xml2->Items->Item;

		$TotalMercant=$item2->Offers->TotalOffers;
		$id=1;
		if ($TotalMercant=="0") {  	
			$store_link_table .='

			';
		}        
		else {
			
			foreach ($item2->Offers->Offer as $itemoffer){
			
				$trcount= $id; $id++;
				$Offer_name=$itemoffer->Merchant->Name;
				$Offer_link=$itemoffer->Merchant->GlancePage;
				$Offer_Rating=$itemoffer->Merchant->AverageFeedbackRating;
				$TotalFeedback = $itemoffer->Merchant->TotalFeedback;
				$Offer_OfferAttributes	=	$itemoffer->OfferAttributes->Condition;
				$Offer_OfferAttributes_note	=	$itemoffer->OfferAttributes->ConditionNote;
				$Offer_OfferListingId	=	$itemoffer->OfferListing->OfferListingId; 
				$Offer_ExchangeId	=	$itemoffer->OfferListing->ExchangeId; 
				$Offer_Price =$itemoffer->OfferListing->Price->FormattedPrice; 
				$Offer_AmountSaved=$itemoffer->OfferListing->AmountSaved->FormattedPrice;
				
				if( $link_cloak ) //$linkreviews = $home_url.'/reviews.php?asin='.$amazon_asin;
					$linkreviews = $home_url.'/reviews-'.$amazon_asin.'.html';
				else $linkreviews = $Offer_link;
				
				
				if ($Offer_name!="Amazon.com"){
					//$store_link_table ='<img src="'.$home_url.'/images/rate/'.start_rating($Offer_Rating).'stars.png" alt="Seller Rating from '.$TotalFeedback.' Reviews"  title="Seller Rating from '.$TotalFeedback.' Reviews" />&nbsp;&nbsp;(<a rel="nofollow" href="'.$linkreviews.'&amp;id='. $trcount.'" title= "Go to review page" > from '.$TotalFeedback .' Reviews</a>)';
					$store_link_table ='<img src="'.$home_url.'/images/rate/'.start_rating($Offer_Rating).'stars.png" alt="Seller Rating from '.$TotalFeedback.' Reviews"  title="Seller Rating from '.$TotalFeedback.' Reviews" />&nbsp;&nbsp;(<a rel="nofollow" href="'.$linkreviews.'" title= "Go to review page" > from '.$TotalFeedback .' Reviews</a>)';
				}
				
			} 
		}      
		$stories .= $store_link_table;
		return $stories;
}


#Tags
function display_tags($item){
/*	global $home_url;
	$Tags = null;
	if ($item->Tags->Tag != null) {
		foreach ($item->Tags->Tag as $t) {
			$Tags[] = (string) $t->Name;
		}
	}
	foreach ($Tags as $t){
		$t = str_replace(' ','-', $t);
		echo ' &nbsp;<a href="'.$home_url.'/'.$t.'/" title="Tags '.$t.'">'.$t.'</a>,&nbsp; ';
	}*/
}


#Accesory
function display_accesory($item){
	$AccesoryProducts= null;
	$title = $item->ItemAttributes->Title;
	if ($item->Accessories->Accessory != null) {
	
		foreach ($item->Accessories->Accessory as $f) {
			$AccesoryProducts[] = (string) $f->ASIN;
		}
		echo '<div class="headtextbg alignleft"><b>More Accessories for '.$title.'</b><br /><br />'; 
		foreach ($AccesoryProducts as $s){
			$count++;
			if ($count > 8) {
				break;
			}
			echo get_accesory($s);
		}
		echo '</div>';
	}
}	



function display_similar($item){
	$SimilarProducts = null;
	if ($item->SimilarProducts->SimilarProduct != null) {
		foreach ($item->SimilarProducts->SimilarProduct as $f) {
			$SimilarProducts[] = (string) $f->ASIN;
		}
		echo '<div class="headtextbg alignleft"><b>Similar Product:</b><br /><br />'; 
		foreach ($SimilarProducts as $s){
			$count++;
			if ($count > 8) {
				break;
			}
			echo get_similar($s);
		}
		echo '</div>'; 
	}
	
}





function get_similar($asin){
global $amazon_zone, $amazon_acc;
	global $home_url,$link_cloak;
	
	$opt = "Images,ItemAttributes,Reviews,OfferSummary";
	$parsed_xml = item_lookup($asin ,$opt, false);
	$item = $parsed_xml->Items->Item;
	
	if( $link_cloak ) $buyUrl = $home_url.'/more-'.$item->ASIN.'.html';
		else $buyUrl = 'http://www.amazon.'.$amazon_zone.'/gp/product/'.$item->ASIN.'/?tag='.$amazon_acc;
	
	$Title = $item->ItemAttributes->Title;
	$SmallImageURL = $item->SmallImage->URL;
	$ImageSet = $item->ImageSets->ImageSet[0];
	if ($SmallImageURL == '') {
		$SmallImageURL = $ImageSet->SmallImage->URL;
	}
	$ListPrice = $item->ItemAttributes->ListPrice->FormattedPrice;
	$SalePrice = $item->Offers->Offer->OfferListing->Price->FormattedPrice;
	
	
	$DetailPageURL = $home_url.'/detail/'.amazon_get_permalink($item->ASIN, $Title);
	
	
	/*$content='
	<div style="width: 135px; margin-bottom: 0px;float:left;line-height:1.3;">
		<div style="margin-left:7px;margin-right:7px;">
			<div style="position: relative; height: 160px;">
				<span style="height: 135px;">
					<a href="'.$buyUrl.'" title="'.$Title.'" target="_blank">
						<div style="margin-bottom:4px;position:relative; width:80px;height:80px;">
							<img src="'.$SmallImageURL .'" alt="'.$Title.'" >
						</div>
						'.truncateStr($Title,30,"..") .'
					</a>
				</span>
				<br />
				<span class="listprice">'.$ListPrice.'</span>
				<span>'.$SalePrice.'</span>
			</div>
		</div>
	</div>';*/
	$content='
	<div style="width: 125px; margin-bottom: 0px;float:left;line-height:1.3;">
		<div style="margin-left:7px;margin-right:7px;">
			<div style="position: relative; height: 160px;">
				<span style="height: 135px;">
					<span onclick="javascript:location=\''.$buyUrl.'\'" class="accesory_similar">
						<div style="margin-bottom:4px;position:relative; width:80px;height:80px;">
							<img src="'.$SmallImageURL .'" alt="'.$Title.'" >
						</div>
						'.truncateStr($Title,30,"..") .'
					</span>
				</span>
				<br />
				<span class="listprice">'.$ListPrice.'</span>
				<span>'.$SalePrice.'</span>
			</div>
		</div>
	</div>
	';
	return $content;
}	

function get_similar2($asin){
	global $home_url;
	
	$opt = "Images,ItemAttributes,Reviews,OfferSummary";
	$parsed_xml = item_lookup($asin ,$opt, false);
	$item = $parsed_xml->Items->Item;
	
	$Title = $item->ItemAttributes->Title;
	$SmallImageURL = $item->SmallImage->URL;
	$ImageSet = $item->ImageSets->ImageSet[0];
	if ($SmallImageURL == '') {
		$SmallImageURL = $ImageSet->SmallImage->URL;
	}
	$ListPrice = $item->ItemAttributes->ListPrice->FormattedPrice;
	$SalePrice = $item->Offers->Offer->OfferListing->Price->FormattedPrice;
	
	$DetailPageURL = $home_url.'/detail/'.amazon_get_permalink($item->ASIN, $Title);
	
	$content='
	<div style="width: 125px; margin-bottom: 0px;float:left;line-height:1.3;">
		<div style="margin-left:7px;margin-right:7px;">
			<div style="position: relative; height: 160px;">
				<span style="height: 135px;">
					<a href="'.$DetailPageURL.'" title="'.$Title.'">
						<div style="margin-bottom:4px;position:relative; width:80px;height:80px;">
							<img src="'.$SmallImageURL .'" alt="'.$Title.'" >
						</div>
						'.truncateStr($Title,30,"..") .'
					</a>
				</span>
				<br />
				<span class="listprice">'.$ListPrice.'</span>
				<span>'.$SalePrice.'</span>
			</div>
		</div>
	</div>
	';
	return $content;
}

function get_accesory($asin){
	global $amazon_zone, $amazon_acc;
	global $home_url,$link_cloak;
	
	$opt = "Images,ItemAttributes,Reviews,OfferSummary";
	$parsed_xml = item_lookup($asin ,$opt, false);
	$item = $parsed_xml->Items->Item;
	
	if( $link_cloak ) $buyUrl = $home_url.'/more-'.$item->ASIN.'.html';
		else $buyUrl = 'http://www.amazon.'.$amazon_zone.'/gp/product/'.$item->ASIN.'/?tag='.$amazon_acc;
	
	$Title = $item->ItemAttributes->Title;
	$SmallImageURL = $item->SmallImage->URL;
	$ImageSet = $item->ImageSets->ImageSet[0];
	if ($SmallImageURL == '') {
		$SmallImageURL = $ImageSet->SmallImage->URL;
	}
	$ListPrice = $item->ItemAttributes->ListPrice->FormattedPrice;
	$SalePrice = $item->Offers->Offer->OfferListing->Price->FormattedPrice;
	
	
	$DetailPageURL = $home_url.'/detail/'.amazon_get_permalink($item->ASIN, $Title);
	
	
	/*$content='
	<div style="width: 135px; margin-bottom: 0px;float:left;line-height:1.3;">
		<div style="margin-left:7px;margin-right:7px;">
			<div style="position: relative; height: 160px;">
				<span style="height: 135px;">
					<a href="'.$buyUrl.'" title="'.$Title.'" target="_blank">
						<div style="margin-bottom:4px;position:relative; width:80px;height:80px;">
							<img src="'.$SmallImageURL .'" alt="'.$Title.'" >
						</div>
						'.truncateStr($Title,30,"..") .'
					</a>
				</span>
				<br />
				<span class="listprice">'.$ListPrice.'</span>
				<span>'.$SalePrice.'</span>
			</div>
		</div>
	</div>';*/
	$content='
	<div style="width: 125px; margin-bottom: 0px;float:left;line-height:1.3;">
		<div style="margin-left:7px;margin-right:7px;">
			<div style="position: relative; height: 160px;">
				<span style="height: 135px;">
					<span onclick="javascript:location=\''.$buyUrl.'\'" class="accesory_similar">
						<div style="margin-bottom:4px;position:relative; width:80px;height:80px;">
							<img src="'.$SmallImageURL .'" alt="'.$Title.'" >
						</div>
						'.truncateStr($Title,30,"..") .'
					</span>
				</span>
				<br />
				<span class="listprice">'.$ListPrice.'</span>
				<span>'.$SalePrice.'</span>
			</div>
		</div>
	</div>
	';
	return $content;
}	
?>